package com.tianxin.workbao.workBao.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.UserDataProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.SPUtils;

import org.json.JSONObject;

/**
 * Created by hacker on 2017/7/1.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener {
    private EditText userNameEditText;
    private EditText passwordEditText;
    private Button loginButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
        }

        initView();

        initData();

        initEvent();
    }

    private void initView() {
        //1.获取 SharedPreferences 保存用户信息，自动登陆 登陆成功后进入 MainActivity
        if (SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0) != 0) {
            String userName = SPUtils.getPrefString(Constant.CURRENT_USER_USERNAME, null);
            String passWord = SPUtils.getPrefString(Constant.CURRENT_USER_PASSWORD, null);
            if (userName == null || passWord == null) {
                return;
            } else {
                UserDataProvide provide = new UserDataProvide(LoginActivity.this, this);
                provide.login(userName, passWord);
            }
        }
        userNameEditText = findViewById(R.id.user_name);
        passwordEditText = findViewById(R.id.password);
        loginButton = findViewById(R.id.login_btn);
    }

    @Override
    public void initData() {
        if (Constant.APP_DEBUG == 1) {
            userNameEditText.setText(getResources().getString(R.string.default_user_name));
            passwordEditText.setText(getResources().getString(R.string.default_password));
        }
    }

    @Override
    public void initEvent() {
        loginButton.setOnClickListener(this);
    }

    @Override
    public void bindView() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                login();
                break;
        }
    }

    private void login() {
        String userName = userNameEditText.getText().toString();
        if (userName.length() < 1) {
            userNameEditText.setText("");
            getSweetAlertDialog(LoginActivity.this).showError("错误", "用户名输入有错");
            return;
        }
        String password = passwordEditText.getText().toString();
        if (password.length() < 6) {
            passwordEditText.setText("");
            getSweetAlertDialog(LoginActivity.this).showError("错误", "密码输入有错");
            return;
        }
        UserDataProvide provide = new UserDataProvide(LoginActivity.this, this);
        provide.login(userName, password);
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("",2000);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {
        saveUserSharedPreferences((JSONObject) result);
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        LoginActivity.this.finish();
    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
